package com.ymx.darling.bean.sql;

import java.util.Date;
import java.util.List;

/**
 * 封装sql语句信息
 * sql语句为拼接后的字符串对象
 * @author 爱java的小于
 */
public class SqlStatement {
    private String sqlStatement;
    private List<Object> paramList;

    public SqlStatement(String sqlStatement, List<Object> paramList){
        this.sqlStatement = sqlStatement;
        this.paramList = paramList;
    }

    public SqlStatement(){}

    public void setParamList(List<Object> paramList) {
        this.paramList = paramList;
    }

    public void setSql(String sqlStatement) {
        this.sqlStatement = sqlStatement;
    }

    public List<Object> getParamList() {
        return paramList;
    }

    public String getSql() {
        return sqlStatement;
    }

    @Override
    public String toString() {
        char[] sqlCharArray =  this.sqlStatement.toCharArray();
        StringBuilder sqlBuilder = new StringBuilder();
        for (int index = 0, paramIndex = 0; index < sqlCharArray.length; ++index) {
            if (sqlCharArray[index] == '?') {
                Object param = this.paramList.get(paramIndex++);
                sqlBuilder.append((param instanceof String || param instanceof Date) ? "'"+param+"'" : param);
            } else {
                sqlBuilder.append(sqlCharArray[index]);
            }
        }
        return sqlBuilder.toString();
    }
}
